package com.hevery.web.wiremock;

import com.github.tomakehurst.wiremock.client.WireMock;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;

/**
 * @author: zhy
 * @description:
 * @date: created on 2017/10/20
 * @modified by:
 */
public class MockServer {
    public static void main(String[] args) throws Exception{
        WireMock.configureFor("127.0.0.1",8062);
        /**把以前的配置都清空*/
        WireMock.removeAllMappings();

        mock("/order/1","01");
        mock("/order/2","02");

    }

    /**
     * 收到某一个url以后，会从文件file中读内容，然后做为响应进行返回
     * @param url
     * @param file
     * @throws IOException
     */
    private static void mock(String url, String file) throws IOException{

        ClassPathResource resource = new ClassPathResource("mock/response/"+file+".txt");

        /**将数组以符号或其他字符串为间隔组成新的字符串； 读取文件数据，以字符串集合返回*/
        String content = StringUtils.join(FileUtils.readLines(resource.getFile(),"UTF-8").toArray(),"\n");

        WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(url)).willReturn(WireMock.aResponse().withBody(content).withStatus(200)));
    }
}
